import request from "../request/index"

// 获取用户数据和群聊数据接口
export const getGourpListAPI = (data) => request.post({
	url: '/ThirdMessage/GetGourpList',
	data
})

// 获取个人用户信息接口
export const getUserInfoAPI = (data) => request.post({
	url: '/ThirdMessage/GetUserInfo',
	data
})

// 文件上传的接口
// export const uploadImagesAPI = (data) => request.post({
// 	url: '/Common/UploadImagesNoAuth',
// 	// header: {
// 	// 	// 需要 multipart/form-data 类型
// 	// 	'Content-Type': 'multipart/form-data'
// 	// },
// 	data
// })

// 获取用户订单图片列表数据接口
export const getStoreIamgeListAPI = (data) => request.post({
	url: '/ThirdMessage/GetStoreIamgeList',
	data
})


// 是否有确认订单
export const getOrderDateilsAPI = (data) => request.post({
	url: '/ThirdMessage/GetOrderDateils',
	data
})

// 设置确认订单
export const setOrderQdAPI = (data) => request.post({
	url: '/ThirdMessage/SetOrderQd',
	data
})

class RestApi {
	constructor() {
		this.users = []
		this.groups = []
	}

	//用户数据示例
	// users = [{
	// 		id: '08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a',
	// 		name: 'Mattie',
	// 		password: '123',
	// 		avatar: '/static/images/Avatar-1.png',
	// 		email: 'Mattie@goeasy.io',
	// 		phone: '138xxxxxxxx',
	// 	},
	// 	{
	// 		id: '3bb179af-bcc5-4fe0-9dac-c05688484649',
	// 		name: 'Wallace',
	// 		password: '123',
	// 		avatar: '/static/images/Avatar-2.png',
	// 		email: 'Wallace@goeasy.io',
	// 		phone: '138xxxxxxxx',
	// 	},
	// ];
	//群数据示例
	// groups = [{
	// 		id: 'group-a42b-47b2-bb1e-15e0f5f9a19a',
	// 		name: '小程序交流群',
	// 		avatar: '/static/images/wx.png',
	// 		userList: [
	// 			'08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a',
	// 			'3bb179af-bcc5-4fe0-9dac-c05688484649',
	// 			'fdee46b0-4b01-4590-bdba-6586d7617f95',
	// 			'33c3693b-dbb0-4bc9-99c6-fa77b9eb763f',
	// 		],
	// 	},
	// ];

	// 订单
	orders = [{
		id: '252364104325',
		url: '/static/images/goods1-1.jpg',
		name: '青桔柠檬气泡美式',
		price: '￥23',
		count: 1
	}, ];


	async fetchHomeFn(params) {
		await this.fetchGetUserInfoAPI(params)
		await this.fetchGetGourpListAPI(params)
		uni.switchTab({
			url: '/pages/conversations'
		})
		const {
			id,
			groupId,
			type,
			userId
		} = params
		if (id) {
			debugger
			if (groupId || type) {
				uni.setStorageSync('tabParams', {
					groupId,
					type
				});
			}
		}
	}

	async fetchGetGourpListAPI(params) {
		const {
			code,
			data
		} = await getGourpListAPI(params)
		if (code == 0) {
			this.users = data.users;
			this.groups = data.groups;
		}
	}

	async fetchGetUserInfoAPI(params) {
		const {
			code,
			data
		} = await getUserInfoAPI(params)
		if (code == 0) {
			const user = data
			uni.$currentUser = user
			uni.setStorageSync('currentUser', user);
		}
	}

	findUsers() {
		return this.users;
	};

	findFriends(user) {
		return this.users.filter((v) => v.id !== user.id);
	}

	findGroups(user) {
		return this.groups.filter((v) => v.userList.find((id) => id === user.id));
	}

	findUser(username, password) {
		return this.users.find((user) => user.name === username && user.password === password);
	}

	getOrderList() {
		return this.orders;
	}

	findGroupById(groupId) {
		return this.groups.find((group) => group.id == groupId);
	}

	findUserById(userId) {
		return this.users.find((user) => user.id == userId);
	}

	findGroupMembers(groupId) {
		let members = [];
		let group = this.groups.find(v => v.id === groupId);
		if (group) {
			members = this.users.filter(user => group.userList.includes(user.id));
		}
		return members;
	}
}

export default new RestApi();